/*
 * @description: http请求服务后端代码
 * @Author: lhr
 * @Date: 2020-10-28 08:58:02
 * @LastEditors: lhr
 * @LastEditTime: 2020-10-30 14:29:13
 */

const http = require("http");
const url = require("url");
const util = require("util");
const fs = require("fs");

const onRequest = function (req, res) {
  console.log(url.parse(req.url, true));
  const urlObj = url.parse(req.url, true);
  const pathname = urlObj.pathname;
  if (pathname === "/detail") {
    res.writeHead("200", { "Content-Type": "application/json" });
    res.end(
      JSON.stringify({
        id: 1,
        name: "张三",
        age: 20,
      }),
      "utf-8"
    );
  } else if (pathname === "/detailCors") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    });
    res.end(
      JSON.stringify({
        id: 1,
        name: "李四",
        age: 20,
        comment: "这是一个允许跨域的连接",
      })
    );
  } else if (pathname === "/login0") {
    res.writeHead("200", {
      "Content-Type": "application/json",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/trunkUpload") {
    var postData = "";
    req.addListener("data", function (data) {
      postData += data;
    });
    req.addListener("end", function () {
      res.writeHead("200", {
        "Content-Type": "application/json",
      });
      res.end(
        JSON.stringify({
          code: 1,
          message: "上传成功",
          fileid: new Date().getTime(),
        })
      );
    });
  } else if (pathname === "/login") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/login2") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Content-Type",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/login3") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "token",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/login4") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Content-Type,token",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/login5") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "http://127.0.0.1:5500",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/login6") {
    res.writeHead("200", {
      "Content-Type": "application/json",
      "Access-Control-Allow-Origin": "http://127.0.0.1:5500",
      "Access-Control-Allow-Credentials": true,
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/loginCookie") {
    console.log("loginCookie", req.headers.cookie);
    res.writeHead("200", {
      "Content-Type": "application/json",
    });
    res.end(
      JSON.stringify({
        code: 1,
        message: "登录成功",
      })
    );
  } else if (pathname === "/fetch") {
    res.writeHead("200", { "Content-Type": "text/html" });
    res.end(fs.readFileSync("./fetch.html"));
  } else if (pathname === "/form") {
    res.writeHead("200", { "Content-Type": "text/html" });
    res.end(fs.readFileSync("./form.html"));
  } else if (pathname === "/kuayu") {
    res.writeHead("200", { "Content-Type": "text/html" });
    res.end(fs.readFileSync("./kuayu.html"));
  } else if (pathname === "/kuayufetch") {
    res.writeHead("200", { "Content-Type": "text/html" });
    res.end(fs.readFileSync("./kuayufetch.html"));
  } else {
    res.writeHead("200", { "Content-Type": "text/plain" });
    res.end("hello node");
  }
};

http.createServer(onRequest).listen(3000);
